package com.ce.pms.log4ce;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;

import com.ce.common.util.BlankUtil;
import com.ce.component.log4ce.aop.web.SpringWebApplicationContextHolder;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.customer.dao.IClientDao;
import com.ce.pms.customer.model.ClientDO;
import com.ce.pms.customer.model.ClientRHouseDO;
import com.ce.pms.customer.service.ICustomerService;
import com.ce.pms.customer.service.IMemberService;
import com.ce.pms.customer.vo.CustomerHouseVO;
import com.ce.pms.customer.vo.CustomerVO;
import com.ce.pms.customer.vo.IndividualVO;
import com.ce.pms.customer.vo.OrganizationVO;
import com.ce.pms.house.dao.IHouseDao;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.house.vo.FlatVO;

public class Log4ceHelper
{
	private ICustomerService customerService ;
	private IHouseService houseService ;
	private IHouseDao houseDao;
	private IMemberService memberService;

	private IClientDao clientDao ;
	private ISysrefcodeService sysrefcodeService ;

	public static Log4ceHelper getInstance(){
		 ApplicationContext context =  SpringWebApplicationContextHolder.getSpringWebApplicationContext();
//		 ApplicationContext context =  new ClassPathXmlApplicationContext(new String[]{"modules/spring.xml","modules/**/spring-conf/*.xml","modules/**/struts-spring-conf/*.xml"});
		 return (Log4ceHelper)context.getBean("log4ceHelper");
	}

	public String getCustomerName(Long ownerId){
		String customerName = "";
		IndividualVO individual = customerService.findIndividualById(ownerId);
		if(!BlankUtil.isBlank(individual)){
			customerName = individual.getParticipantName();
		}
		else {
			OrganizationVO organizationVO = customerService.findOrganizationById(ownerId);
			if(!BlankUtil.isBlank(organizationVO))
			{
				customerName =organizationVO.getParticipantName();
			}
		}
		return customerName;
	}

	public FlatVO getFlatVOById(Long id){
		return (FlatVO)houseService.getFlatHouseById(id);
	}

	public String getHouseNumber(Long houseId){
		//HouseVO houseVO = houseService.getFlatHouseById(houseId);
		HouseDomain house = houseDao.findById(houseId);
		return house ==null ? "" : house.getHouseNumber();
	}

	public CustomerVO getCustomerById(Long id, String type){
		if(CustomerVO.INDIVIDUAL_CODE.equals(type))
		{
			return customerService.findIndividualById(id);

		}
		else if(CustomerVO.ORGNIZATION_CODE.equals(type))
		{
			return customerService.findOrganizationById(id);
		}
		return null;
	}

	public String collectionToString(Collection<String> values){
		if(BlankUtil.isBlank(values))
		{
			return "";
		}
		Object[] s = values.toArray();
		StringBuffer temp = new StringBuffer();
		for(int i = 0; i < s.length; i++)
		{
			temp.append(s[i]);
			if(i<s.length-1)
			{
				temp.append(",");
			}
		}
		return temp.toString();
	}

	public String mapValuesToString(Map<?,String> map){
		Collection<String> values = map.values();
		return collectionToString(values);
	}

	public String genHouseNumbers(List<CustomerHouseVO> houseList ){
		if(BlankUtil.isBlank(houseList))
		{
			return "";
		}
		if(houseList.size()==1)
		{
			return houseList.get(0).getHouseNumber();
		}else{
			StringBuffer sb = new StringBuffer();
			sb.append("{");
			for(CustomerHouseVO customerHouseVO : houseList)
			{
				sb.append(customerHouseVO.getHouseNumber()).append(",");
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append("}");
			return sb.toString();
		}
	}

	/**
	 * 取得status之外的房子集合
	 * @param houses
	 * @param status
	 * @return
	 */
	public List<CustomerHouseVO> getHousesFilterByStatus(List<CustomerHouseVO> houses, String status){
		List<CustomerHouseVO> list = new ArrayList<CustomerHouseVO>();
		if(!BlankUtil.isBlank(houses))
		{
			for(CustomerHouseVO house : houses)
			{
				if(status.equals(house.getOpratorStatus()))
				{
					continue;
				}
				list.add(house);
			}
		}
		return list;
	}

	public List<CustomerHouseVO> getSourceHouses(Long ownerId,String clientTypeCode){
		SysrefcodeDO clientType = sysrefcodeService.getSysrefCodesByCode(SysrefcodeDO.CS_CLIENT_CLIENT_TYPE_CODE, clientTypeCode);
		ClientDO  client = clientDao.queryClientByParticipantAndClientType(ownerId, clientType.getId());
		 Set<ClientRHouseDO> houses = client.getClientRHouses();
		 List<CustomerHouseVO> list = new ArrayList<CustomerHouseVO>();
		 if(!BlankUtil.isBlank(houses))
		 {
			for(ClientRHouseDO house : houses)
			{
				if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(house.getValidity())){
					CustomerHouseVO customerHouseVO = new CustomerHouseVO();
					customerHouseVO.setHouseNumber(house.getHouse().getHouseNumber());
					customerHouseVO.setHouseId(house.getHouse().getKeyword());
					list.add(customerHouseVO);
				}
			}
		}
		 return list;
	}

	public int getEditMode(List<CustomerVO> customerList){
		if(BlankUtil.isBlank(customerList))
		{
			return 0;
		}
		return customerList.size();
	}

	public ICustomerService getCustomerService()
	{
		return customerService;
	}

	public IHouseService getHouseService()
	{
		return houseService;
	}

	public void setCustomerService(ICustomerService customerService)
	{
		this.customerService = customerService;
	}

	public void setHouseService(IHouseService houseService)
	{
		this.houseService = houseService;
	}

	public IMemberService getMemberService()
	{
		return memberService;
	}

	public void setMemberService(IMemberService memberService)
	{
		this.memberService = memberService;
	}

	public IClientDao getClientDao()
	{
		return clientDao;
	}

	public ISysrefcodeService getSysrefcodeService()
	{
		return sysrefcodeService;
	}

	public void setClientDao(IClientDao clientDao)
	{
		this.clientDao = clientDao;
	}

	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService)
	{
		this.sysrefcodeService = sysrefcodeService;
	}

	public IHouseDao getHouseDao() {
		return houseDao;
	}

	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}


}
